<?php

class Controller_User_Template extends Controller_Template {
	public $template = 'user/template';
	
	public function before() {
		parent::before();

		if($this->request->is_ajax()) {
			$this->auto_render = FALSE;
		}
		
		if($this->auto_render) {
			if($user = Auth::instance()->get_user()) {
				// User is found.
				$login = _('Logged in:  '.$user->username);
				$user_info = View::factory('template/userinfo');
				$user_info->set('user', $user);
				
			}
			else {
				// user is not found;
				$login = View::factory('user/login');	
				$user_info = View::factory('template/login');
			}

			$categories = ORM::factory('category')
				->where('parent_category_id', '=', 0)
				->order_by('name', 'asc')
				->find_all();
			
			$sidebar = View::factory('user/sidebar');
			$sidebar->set('categories', $categories);
			
			$search = View::factory('user/search');
			$categories = ORM::factory('category')
					->where('parent_category_id', '=', 0)
					->find_all();
			
			$cart = View::factory('template/cart');
			$item = Cookie::get('items');
			$item = explode(',', $item);
			$items = array();			
			
			foreach($item as $i):
				$items[] = ORM::factory('inventoryitem', $i);
			endforeach;
			$cart->set('items', $items);
			
			if($item[0] == NULL):
				$num_items = 0;				
			else:
				$num_items = count($items);
			endif;
			
			$cart->set('num_items', $num_items);
			
			$search->categories = $categories;
			$this->template->search = $search;
			$this->template->simplesearch = View::factory('template/search');
			$this->template->cart = $cart;
			$this->template->sidebar = $sidebar;
			
			$promotionsview = View::factory('template/currentpromotions');
			
			$promotions = ORM::factory('promotion')
				->where('starting_date', '<=', strtotime("now"))
				->and_where('ending_date', '>=', strtotime("now"))
				->find_all();
			$promotionsview->set('promotions', $promotions);
			
			$recentproducts = View::factory('template/recentproducts');
			
			$items = ORM::factory('item')
				->where('discontinued', '=', 0)
				->order_by('id', 'desc')
				->limit(9)
				->find_all();

			$recentproducts->set('items', $items);
			
			$this->template->currentpromotions = '';
			$this->template->user_info = $user_info;
			$this->template->title = 'Sporting Goods Company';
			$this->template->content = $recentproducts;
			$this->template->login = $login;
		}
		
	}
	
	public function after()
	{
		/*
		if($this->auto_render == FALSE) {
			// We have ajax or internal request here
			$this->template = $this->content;
		}
		else {
			// We have regular http request for a page
			$this->template = View::factory($this->template)
				->set('content', $this->content);
		}
		*/
		
		/*
		if ($this->auto_render)
		{
			$styles = array(
					'media/css/screen.css' => 'screen, projection',
					'media/css/print.css' => 'print',
					'media/css/style.css' => 'screen',
			);
	
			$scripts = array(
					'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
			);
	
			$this->template->styles = array_merge( $this->template->styles, $styles );
			$this->template->scripts = array_merge( $this->template->scripts, $scripts );
		}
		*/
		parent::after();
	}
}